﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace WebPiket3Csv
{
    /// <summary>
    /// Логика взаимодействия для Page3_Table.xaml
    /// </summary>
    public partial class Page3_Table : Page
    {
        public Page3_Table()
        {
            InitializeComponent();

            GenTable();
            tbl.ItemsSource = PrjProperty.pikets;
        }

        public void GenTable()
        {
            PrjProperty.pikets = new ObservableCollection<Piket>();
			int iter = 0;
			#region Read Piket
			using (StringReader reader = new StringReader(PrjProperty.txtDataPkt))
			{
				string line;
				while ((line = reader.ReadLine()) != null)
				{
					iter++;
					string[] values = line.Split('\t');
					Piket pkt = new Piket();
					try
					{
						bool isPiket = false;
						isPiket = PrjProperty.IsDouble(values[6]) && PrjProperty.IsDouble(values[10]) && PrjProperty.IsDouble(values[13]);

						if (isPiket)
						{
							pkt.From = values[0];
							pkt.To = values[1];
							pkt.Date = values[3];
							pkt.Length = PrjProperty.StrToDouble(values[6]);
							pkt.Azimuth = PrjProperty.StrToDouble(values[10]);
							pkt.Inc = PrjProperty.StrToDouble(values[13]);

							pkt.Left = PrjProperty.StrToDouble(values[15]);
							pkt.Right = PrjProperty.StrToDouble(values[16]);
							pkt.Up = PrjProperty.StrToDouble(values[17]);
							pkt.Down = PrjProperty.StrToDouble(values[18]);

							pkt.Dx = Math.Round(pkt.Length * Math.Cos((pkt.Azimuth - 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180), 2);
							pkt.Dy = Math.Round(-pkt.Length * Math.Sin((pkt.Azimuth - 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180), 2);
							pkt.Dz = Math.Round(pkt.Length * Math.Sin((pkt.Inc) * Math.PI / 180), 2);

							if (pkt.To != "Wall"
							 && pkt.To != "Floor/ceiling"
							 && pkt.To != "Detail")
							{
								PrjProperty.pikets.Add(pkt);
							}
							else
							{
								PrjProperty.pikets[PrjProperty.pikets.Count - 1].Splays.Add(new Splay() { From = pkt.From, To = pkt.To, Azimuth = pkt.Azimuth, Inc = pkt.Inc, Length = pkt.Length });
							}
						}
					}
					catch
					{ }
				}
			} 
			#endregion
			#region ReadStations
			using (StringReader reader = new StringReader(PrjProperty.txtDataSts))
			{
				string line;
				while ((line = reader.ReadLine()) != null)
				{
					string[] values = line.Split('\t');
					//pkt - это условный пикет,у которого значения from и to совпадают, нужен для подстановки
					Piket pkt = new Piket();
					try
					{
						pkt.From = values[0];
						pkt.To = values[0];
						pkt.XFrom = PrjProperty.StrToDouble(values[2]);
						pkt.YFrom = PrjProperty.StrToDouble(values[3]);
						pkt.ZFrom = PrjProperty.StrToDouble(values[4]);
						pkt.XTo = pkt.XFrom;
						pkt.YTo = pkt.YFrom;
						pkt.ZTo = pkt.ZFrom;

						foreach (Piket piket in PrjProperty.pikets)
						{
							if (piket.From == pkt.From)
							{
								piket.XFrom = pkt.XFrom;
								piket.YFrom = pkt.YFrom;
								piket.ZFrom = pkt.ZFrom;
							}
							if (piket.To == pkt.To)
							{
								piket.XTo = pkt.XTo;
								piket.YTo = pkt.YTo;
								piket.ZTo = pkt.ZTo;
							}
						}
					}
					catch
					{ }
				}
			} 
			#endregion
			#region LRUD2
			foreach (Piket pkt in PrjProperty.pikets)
			{
				foreach (Piket pktFind in PrjProperty.pikets)
				{
					if (pkt.To == pktFind.From)
					{
						pkt.Left2 = pktFind.Left;
						pkt.Right2 = pktFind.Right;
						pkt.Up2 = pktFind.Up;
						pkt.Down2 = pktFind.Down;
					}
				}
			} 
			#endregion
        }

        public string GenMs()
        {
			string str = "-- GenByWebPiket3 at " + DateTime.Now.ToString() + "\r";
            string strSurvey = "-- GenByWebPiket3 at " + DateTime.Now.ToString() + "\r";

			#region Sample .ms
			/*
            -- GenByWebPiket3 at 15.12.2012 16:37:17
            fn DrawTrk0Seg0 no  = 
            (
                ss = SplineShape pos: [0,0,30.9] 

                addNewSpline ss 
                addKnot ss 1 #corner #line [0,0,30.9]
                addKnot ss 1 #corner #line [100,60,20]

                addNewSpline ss 	
                addKnot ss 2 #corner #line [10,30,0]
                addKnot ss 2 #corner #line [100,100,100]
             
                updateShape ss 
                ss 
             ) 
            newSpline = DrawTrk0Seg0 0 
             */
			
			#endregion
            str += "fn DrawPiketLine no  = \r"
                + "( \r"
                + "ss = SplineShape pos: [0,0,0] \r\r";
            strSurvey += "fn DrawSurveyLine no  = \r"
                + "( \r"
                + "ssurv = SplineShape pos: [0,0,0] \r\r";
            int countLine = 0;
            int countLineSurv = 0;
			ObservableCollection<Piket> pikets = new ObservableCollection<Piket>();
			if (tbl.SelectedItems.Count > 1)
			{
				foreach (var data in tbl.SelectedItems)
				{
					pikets.Add(data as Piket);
				}
			}
			else
				pikets = PrjProperty.pikets;
            foreach (Piket pkt in pikets)
            {
                #region pnt
                PointF pntD = new PointF()
                {
                    X = Math.Round(pkt.XFrom, 2),
                    Y = Math.Round(pkt.YFrom, 2),
                    Z = Math.Round(pkt.ZFrom - pkt.Down, 2)
                };
                PointF pntU = new PointF()
                {
                    X = Math.Round(pkt.XFrom, 2),
                    Y = Math.Round(pkt.YFrom, 2),
                    Z = Math.Round(pkt.ZFrom + pkt.Up, 2)
                };
                PointF pntL = new PointF()
                {
                    X = Math.Round(pkt.XFrom + pkt.Left * Math.Cos((pkt.Azimuth - 90 - 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180), 2),
                    Y = Math.Round(pkt.YFrom - pkt.Left * Math.Sin((pkt.Azimuth - 90 - 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180), 2),
                    Z = Math.Round(pkt.ZFrom, 2)
                };
                PointF pntR = new PointF()
                {
                    X = Math.Round(pkt.XFrom + pkt.Right * Math.Cos((pkt.Azimuth - 90 + 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180), 2),
                    Y = Math.Round(pkt.YFrom - pkt.Right * Math.Sin((pkt.Azimuth - 90 + 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180), 2),
                    Z = Math.Round(pkt.ZFrom, 2)
                };
                PointF pntD2 = new PointF()
                {
                    X = Math.Round(pkt.XTo, 2),
                    Y = Math.Round(pkt.YTo, 2),
                    Z = Math.Round(pkt.ZTo - pkt.Down2, 2)
                };
                PointF pntU2 = new PointF()
                {
                    X = Math.Round(pkt.XTo, 2),
                    Y = Math.Round(pkt.YTo, 2),
                    Z = Math.Round(pkt.ZTo + pkt.Up2, 2)
                };
                PointF pntL2 = new PointF()
                {
                    X = Math.Round(pkt.XTo + pkt.Left2 * Math.Cos((pkt.Azimuth - 90 - 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180), 2),
                    Y = Math.Round(pkt.YTo - pkt.Left2 * Math.Sin((pkt.Azimuth - 90 - 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180), 2),
                    Z = Math.Round(pkt.ZTo, 2)
                };
                PointF pntR2 = new PointF()
                {
                    X = Math.Round(pkt.XTo + pkt.Right2 * Math.Cos((pkt.Azimuth - 90 + 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180), 2),
                    Y = Math.Round(pkt.YTo - pkt.Right2 * Math.Sin((pkt.Azimuth - 90 + 90) * Math.PI / 180) * Math.Cos((pkt.Inc) * Math.PI / 180), 2),
                    Z = Math.Round(pkt.ZTo, 2)
                };
                #endregion
                #region Piket
                countLineSurv++;
                strSurvey += "addNewSpline ssurv  \r";
                strSurvey += String.Format("--Piket: {0} - {1} \r", pkt.From, pkt.To);
                strSurvey += String.Format("addKnot ssurv {0} #corner #line [{1},{2},{3}] \r", countLineSurv, PrjProperty.DoubleToStr(pkt.XFrom * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.YFrom * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.ZFrom * PrjProperty.propertyScale));
                strSurvey += String.Format("addKnot ssurv {0} #corner #line [{1},{2},{3}] \r", countLineSurv, PrjProperty.DoubleToStr(pkt.XTo * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.YTo * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.ZTo * PrjProperty.propertyScale));
                #endregion
                #region Up-Down
                countLineSurv++;
                strSurvey += "addNewSpline ssurv  \r";
                strSurvey += String.Format("--Piket: {0} - {1} Up-Down \r", pkt.From, pkt.To);
                strSurvey += String.Format("addKnot ssurv {0} #corner #line [{1},{2},{3}] \r", countLineSurv, PrjProperty.DoubleToStr(pntU.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntU.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntU.Z * PrjProperty.propertyScale));
                strSurvey += String.Format("addKnot ssurv {0} #corner #line [{1},{2},{3}] \r", countLineSurv, PrjProperty.DoubleToStr(pkt.XFrom * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.YFrom * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.ZFrom * PrjProperty.propertyScale));
                strSurvey += String.Format("addKnot ssurv {0} #corner #line [{1},{2},{3}] \r", countLineSurv, PrjProperty.DoubleToStr(pntD.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntD.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntD.Z * PrjProperty.propertyScale));
                #endregion
                #region Left-Right
                countLineSurv++;
                strSurvey += "addNewSpline ssurv  \r";
                strSurvey += String.Format("--Piket: {0} - {1} Left-Right \r", pkt.From, pkt.To);
                strSurvey += String.Format("addKnot ssurv {0} #corner #line [{1},{2},{3}] \r", countLineSurv, PrjProperty.DoubleToStr(pntL.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntL.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntL.Z * PrjProperty.propertyScale));
                strSurvey += String.Format("addKnot ssurv {0} #corner #line [{1},{2},{3}] \r", countLineSurv, PrjProperty.DoubleToStr(pkt.XFrom * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.YFrom * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.ZFrom * PrjProperty.propertyScale));
                strSurvey += String.Format("addKnot ssurv {0} #corner #line [{1},{2},{3}] \r", countLineSurv, PrjProperty.DoubleToStr(pntR.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntR.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntR.Z * PrjProperty.propertyScale));
                #endregion

                #region Contur and Splay
                countLine++;
                str += "addNewSpline ss  \r";
                str += String.Format("--Piket: {0} - {1} \r", pkt.From, pkt.To);
                str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pkt.XFrom * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.YFrom * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntD.Z * PrjProperty.propertyScale));
                str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pkt.XTo * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.YTo * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntD2.Z * PrjProperty.propertyScale));


                countLine++;
                str += "addNewSpline ss  \r";
                str += String.Format("--Piket: {0} - {1} Contur \r", pkt.From, pkt.To);
                str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pntL.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntL.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntU.Z * PrjProperty.propertyScale));
                str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pntL.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntL.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.ZFrom * PrjProperty.propertyScale));
                str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pntL.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntL.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntD.Z * PrjProperty.propertyScale));
                str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pkt.XFrom * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.YFrom * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntD.Z * PrjProperty.propertyScale));
                str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pntR.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntR.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntD.Z * PrjProperty.propertyScale));
                str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pntR.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntR.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.ZFrom * PrjProperty.propertyScale));
                str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pntR.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntR.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntU.Z * PrjProperty.propertyScale));
                #endregion

                #region Splay
                int nosplay = 0;
                if (PrjProperty.propertyIsSplay)
                    foreach (Splay spl in pkt.Splays)
                    {
                        nosplay++;
                        PointF pntSpl = new PointF();
                        PointF pntSplUp = new PointF();
                        PointF pntSplDown = new PointF();
                        #region count pntSpl
                        if (spl.From == pkt.From)
                        {
                            pntSpl = new PointF()
                            {
                                X = Math.Round(pkt.XFrom + spl.Length * Math.Cos((spl.Azimuth - 90 ) * Math.PI / 180) * Math.Cos((spl.Inc) * Math.PI / 180), 2),
                                Y = Math.Round(pkt.YFrom - spl.Length * Math.Sin((spl.Azimuth - 90 ) * Math.PI / 180) * Math.Cos((spl.Inc) * Math.PI / 180), 2),
                                Z = Math.Round(pkt.ZFrom + spl.Length * Math.Sin((spl.Inc) * Math.PI / 180), 2)
                            };
                            pntSplUp = new PointF()
                            {
                                X = Math.Round(pntSpl.X, 2),
                                Y = Math.Round(pntSpl.Y, 2),
                                Z = Math.Round(pntSpl.Z + pkt.Up, 2)
                            };
                            pntSplDown = new PointF()
                            {
                                X = Math.Round(pntSpl.X, 2),
                                Y = Math.Round(pntSpl.Y, 2),
                                Z = Math.Round(pntSpl.Z - pkt.Down, 2)
                            };
                        }
                        if (spl.From == pkt.To)
                        {
                            pntSpl = new PointF()
                            {
                                X = Math.Round(pkt.XTo + spl.Length * Math.Cos((spl.Azimuth - 90 ) * Math.PI / 180) * Math.Cos((spl.Inc) * Math.PI / 180), 2),
                                Y = Math.Round(pkt.YTo - spl.Length * Math.Sin((spl.Azimuth - 90 ) * Math.PI / 180) * Math.Cos((spl.Inc) * Math.PI / 180), 2),
                                Z = Math.Round(pkt.ZTo + spl.Length * Math.Sin((spl.Inc) * Math.PI / 180), 2)
                            };
                            pntSplUp = new PointF()
                            {
                                X = Math.Round(pntSpl.X, 2),
                                Y = Math.Round(pntSpl.Y, 2),
                                Z = Math.Round(pntSpl.Z + pkt.Up2, 2)
                            };
                            pntSplDown = new PointF()
                            {
                                X = Math.Round(pntSpl.X, 2),
                                Y = Math.Round(pntSpl.Y, 2),
                                Z = Math.Round(pntSpl.Z - pkt.Down2, 2)
                            };
                        }
                        #endregion
                        countLineSurv++;
                        strSurvey += "addNewSpline ssurv  \r";
                        strSurvey += String.Format("--Piket: {0} - {1} Splay{2} \r", pkt.From, pkt.To, nosplay);
                        strSurvey += String.Format("addKnot ssurv {0} #corner #line [{1},{2},{3}] \r", countLineSurv, PrjProperty.DoubleToStr(pkt.XTo * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.YTo * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pkt.ZTo * PrjProperty.propertyScale));
                        strSurvey += String.Format("addKnot ssurv {0} #corner #line [{1},{2},{3}] \r", countLineSurv, PrjProperty.DoubleToStr(pntSpl.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntSpl.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntSpl.Z * PrjProperty.propertyScale));

                        countLine++;
                        str += "addNewSpline ss  \r";
                        str += String.Format("--Piket: {0} - {1} \r", pkt.From, pkt.To);
                        str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pntSplUp.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntSplUp.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntSplUp.Z * PrjProperty.propertyScale));
                        if (PrjProperty.propertyIsSplayCentarlPoint)
                            str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pntSpl.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntSpl.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntSpl.Z * PrjProperty.propertyScale));
                        str += String.Format("addKnot ss {0} #corner #line [{1},{2},{3}] \r", countLine, PrjProperty.DoubleToStr(pntSplDown.X * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntSplDown.Y * PrjProperty.propertyScale), PrjProperty.DoubleToStr(pntSplDown.Z * PrjProperty.propertyScale));
                    }
                #endregion
                str += "\r";
            }
            str += "updateShape ss  \r"
                + "ss.wirecolor =[255,200,0] \r ss \r) \r"
                + "newSpline = DrawPiketLine 0 \r";
            strSurvey += "updateShape ssurv  \r"
                + "ssurv.wirecolor =[100,100,100] \r ssurv \r) \r"
                + "newSpline = DrawSurveyLine 0 \r";

            return String.Format("{0}\r\r\r{1}", strSurvey, str);
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string str = GenMs();
			DlgEditWnd editWnd = new DlgEditWnd();
			editWnd.txt.Text = str;
            //System.Windows.Clipboard.SetText(str);
            editWnd.Show();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            DlgOptions dlgOpt = new DlgOptions();
            dlgOpt.GetVal();
            dlgOpt.ShowDialog();
        }
    }
}
